package com.coding.controller;

import com.alibaba.fastjson2.JSON;
import com.coding.dto.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/like")
public class VideoLikeController {

    @Autowired
    RedisTemplate<String, Object> myRedis;


    @RequestMapping("/like")
    public RestBean likeVideo(Long videoId, Long userId) {
        Map<String, Timestamp> data = new HashMap<>();


        System.out.println("videoId: " + videoId + "\n userId: " + userId);

        String userId_str = Long.toString(userId);

        if (myRedis.opsForHash().hasKey("VideoUpdateData", userId_str )) {
            System.out.println("存在数据！");
        } else {
            Date date = new Date();
            Timestamp stamp = new Timestamp(date.getTime());
            data.put(userId_str, stamp);
            System.out.println(data);
            Object jsonData = JSON.toJSON(data);
            myRedis.opsForHash().put("VideoUpdateData", videoId + "", jsonData);
        }
        return RestBean.success("点赞成功！");
    }


    @RequestMapping("/disLike")
    public RestBean dislikeVideo(Long videoId, Long userId) {

        String videoId_str = Long.toString(videoId);

        System.out.println("videoId: " + videoId + "\n userId: " + userId);
        if (myRedis.opsForHash().hasKey("VideoUpdateData", videoId_str)) {
            System.out.println("存在数据！");
            myRedis.opsForHash().delete("VideoUpdateData", videoId_str);
        }

        return RestBean.success("取消点赞成功！");
    }


}
